﻿#include<iostream>
#include<thread>

#include"CommandManage.h"

#include"CommandConsole.h"

CommandConsole::CommandConsole(VTKRenderer& renderer)
    : m_Renderer(renderer)
{

}

CommandConsole::~CommandConsole()
{
    StopCommandLoop();
}

void CommandConsole::StartCommandLoop()
{
    m_CommandLoopRunning = true;
    m_CommandThread = std::thread([this]() 
    {        
        while (m_CommandLoopRunning)
        {
            std::string command;
            std::cout << "Enter command (help for list): ";
            std::getline(std::cin, command);

            // 加锁确保线程安全
            std::lock_guard<std::mutex> lock(m_CommandMutex);            
            if (command == "help") 
            {
                std::cout << "Commands:\n"
                          << "  trapezoid - 构建等腰梯形\n"
                          << "  extrude   - 几何拉伸\n"
                          << "  clear     - 清空所有模型\n"
                          << "  show      - 显示所有模型\n"
                          << "  help      - 显示帮助\n";
            }
            else if(CommandManage::Command(command))
            {
                // nothing
            }
            else 
            {
                std::cout << "未知命令，输入 'help' 查看可用命令.\n";
            }
        }
    });
}

void CommandConsole::StopCommandLoop()
{
    m_CommandLoopRunning = false;
    if (m_CommandThread.joinable()) 
    {
        m_CommandThread.join();
    }
}
